#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;

    int num, award;
    string str;

    while (n--)
    {
        int score = 0;
        cin >> num >> award;
        cin >> str;

        char prev = str[1], pprev = str[0];
        for (int i = 0; i < num; i++)
        {
            if (str[i] == 'W')
                score++;
            else
                score--;
            if (i > 1)
            {
                if (prev == pprev && prev == str[i] && pprev == str[i]&&str[i]=='W')
                    score += award-1;
                prev = str[i];
                pprev = str[i - 1];
            }
        }
        cout << score << endl;
    }
    return 0;
}

